package com.buding.fx.utils;

import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

import java.util.*;

/**
 * @author mingdongwei-jwk
 * @date 2023/07/10 14:36
 * @class com.buding.fx.utils
 * @Description
 */
public class CaiPiaoCreateThread extends Thread {
        private BorderPane borderPane;
        private Label lable;

        //设置字体间距5px
        private String lableStyle = "-fx-text-fill: #a09999; -fx-font-weight: normal; -fx-font-size:10px;";

        public CaiPiaoCreateThread(BorderPane borderPane, Label lable) {
            this.borderPane = borderPane;
            this.lable = lable;
        }

        @Override
        public void run() {
           if (borderPane!=null && lable!=null){
               lable.setStyle("-fx-background-color: #ffffff; -fx-text-fill: #ee0707; -fx-font-weight: bold; -fx-font-size:15px;");
               System.out.println("click label: "+lable.getText());
               FlowPane flowPane =(FlowPane) borderPane.getTop();
               flowPane.getChildren().forEach(node -> {
                   if (node instanceof Label){
                       Label curLabel = (Label) node;
                        String userData = curLabel.getText();
                        if (!userData.equals(lable.getText())){
                            curLabel.setStyle("-fx-text-fill: #2a2828;-fx-font-size:14px;");
                        }
                   }
               });
               Node centerNode  = createCenterNode(lable);
               borderPane.setCenter(centerNode);
           }
        }

    private Node createCenterNode(Label lable) {
           String text = lable.getText();
           VBox vBox = new VBox();
           vBox.setPrefWidth(borderPane.getPrefWidth());
           vBox.setPrefHeight(borderPane.getPrefHeight()*0.6);
           vBox.setAlignment(Pos.TOP_CENTER);
           vBox.setStyle("-fx-background-color: #ffffff; -fx-border-color: #c2bfbf; -fx-border-width: 1px 1px 0px 1px;");
           if (text.equals("复试选号")) {
               initFSBox(vBox);
           }else   if (text.equals("单式选号")) {
               initDSBox(vBox);
           }else   if (text.equals("胆拖选号")) {
               initDTBox(vBox);
           }else  if (text.equals("定胆杀号")) {
               initDDBox(vBox);
           }
           return vBox;
    }

    private void initFSBox(VBox vBox){
        Label label = new   Label("至少选择 6 个红球和 1 个蓝球，红球最多可选 20 个。");
        //设置label字体间距5px
        label.setPrefWidth(vBox.getPrefWidth());
        label.setStyle(lableStyle);
        vBox.getChildren().add(label);
        VBox.setMargin(label,new Insets(10,0,0,10));
        initLBGridPane(vBox);

    }

    private void initDSBox(VBox vBox){
        Label label = new   Label("至少选择6个红球和1个蓝球，红球最多可以选择20");
        label.setPrefWidth(vBox.getPrefWidth());
        label.setStyle(lableStyle);
        vBox.getChildren().add(label);
        VBox.setMargin(label,new Insets(10,0,0,10));
        initLBGridPane(vBox);
    }

    private void initDTBox(VBox vBox){
        Label label = new   Label("红球胆码只能选择 1 到 5 个，红球拖码最多可选16个，蓝球至少选择 1 个。");
        label.setPrefWidth(vBox.getPrefWidth());
        label.setStyle(lableStyle);
        vBox.getChildren().add(label);
        VBox.setMargin(label,new Insets(10,0,0,10));
        initLBGridPane(vBox);
    }

    private void initDDBox(VBox vBox){
        Label label = new   Label("温馨提示：同一号码点击一下为定胆、点击两下为杀号、点击三下为还原。");
        label.setPrefWidth(vBox.getPrefWidth());
        label.setStyle(lableStyle);
        vBox.getChildren().add(label);
        VBox.setMargin(label,new Insets(10,0,0,10));
        initLBGridPane(vBox);
    }


    private void initLBGridPane(VBox vBox){
       HBox hBox = new HBox();
       hBox.setPrefWidth(vBox.getPrefWidth());
       hBox.setPrefHeight(vBox.getPrefHeight()*0.8);
      // VBox.setMargin(hBox,new Insets(10,0,0,0));

       GridPane redGridePane = new GridPane();
       redGridePane.setPrefWidth(hBox.getPrefWidth()*0.6);
       redGridePane.setAlignment(Pos.TOP_LEFT);
      // redGridePane.setStyle("-fx-background-color: #7d7878;");
       Label redLabel = new Label("【红球选择区】");
       redLabel.setFont(Font.font(15));
       redLabel.setTextFill(Color.valueOf("red"));
       redGridePane.add(redLabel,0,0);

       FlowPane redFlowPane = new FlowPane();
       redFlowPane.maxWidth(redGridePane.getPrefWidth());
       redFlowPane.setMaxHeight(50);
       for (int i=1;i<=33; i++){
           Circle circle = new Circle();
           circle.setRadius(15);
           circle.setFill(Color.web("#E7E2E2FF"));
           circle.setStroke(Color.valueOf("#D0CDCDFF"));
           circle.setStrokeWidth(1);
           circle.setId(i+"");
           StackPane stackPane = new StackPane();
           Text text = new Text(i+"");
           text.setStyle("-fx-font-size: 12px; -fx-font-weight: bold; -fx-fill: #4e4a4a; -fx-font-family: 'Microsoft YaHei'; -fx-font-smoothing-type: lcd; -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.2) , 1, 0.0 , 0 , 1 ); -fx-text-alignment: center; -fx-text-origin: center; -fx-font-size: 14");
           stackPane.getChildren().addAll(circle,text);
           stackPane.getProperties().put("click",false);
           stackPane.setOnMousePressed(new EventHandler<MouseEvent>() {
               @Override
               public void handle(MouseEvent event) {
                   Circle circle = (Circle)stackPane.getChildren().get(0);
                   //circle.setStyle("-fx-border-color: #1678ff;");
                   Text textNode = (Text)stackPane.getChildren().get(1);
                   boolean click = (boolean) stackPane.getProperties().get("click");
                   if (click){
                       circle.setFill(Color.web("#E7E2E2FF"));
                       text.setStyle("-fx-font-size: 12px; -fx-font-weight: bold; -fx-fill: #4e4a4a; -fx-font-family: 'Microsoft YaHei'; -fx-font-smoothing-type: lcd; -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.2) , 1, 0.0 , 0 , 1 ); -fx-text-alignment: center; -fx-text-origin: center; -fx-font-size: 14");
                   }else{
                       circle.setFill(Color.web("#F50606FF"));
                      // circle.setStyle("-fx-border-color: #f50606;");
                       textNode.setStyle("-fx-font-size: 12px; -fx-font-weight: bold; -fx-fill: #f5f1f1; -fx-font-family: 'Microsoft YaHei'; -fx-font-smoothing-type: lcd; -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.2) , 1, 0.0 , 0 , 1 ); -fx-text-alignment: center; -fx-text-origin: center; -fx-font-size: 14");
                   }
                   stackPane.getProperties().put("click",!click);
               }
           });


           redFlowPane.getChildren().add(stackPane);
           FlowPane.setMargin(stackPane,new Insets(5,10,0,0));
       }
       redGridePane.add(redFlowPane,0,1);
       //增加随机红球选择下拉选项
        HBox randomHbox1 = new HBox();
        Button r = new Button("随机选择");
        r.setStyle("-fx-background-color: #FCF6D8FF; -fx-text-fill: #181717; -fx-font-size: 12px; -fx-border-color: #e7e5e5; -fx-border-radius: 1px; -fx-border-width: 1px; -fx-border-style: solid;");
        r.getProperties().put("num","5"); //默认值
        r.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //清空上一次的选择结果
                redFlowPane.getChildren().forEach(e->{
                    StackPane stackPane = (StackPane) e;
                    Circle circle = (Circle)stackPane.getChildren().get(0);
                    //circle.setStyle("-fx-border-color: #1678ff;");
                    Text textNode = (Text)stackPane.getChildren().get(1);
                    circle.setFill(Color.web("#E7E2E2FF"));
                    textNode.setStyle("-fx-font-size: 12px; -fx-font-weight: bold; -fx-fill: #4e4a4a; -fx-font-family: 'Microsoft YaHei'; -fx-font-smoothing-type: lcd; -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.2) , 1, 0.0 , 0 , 1 ); -fx-text-alignment: center; -fx-text-origin: center; -fx-font-size: 14");
                    stackPane.getProperties().put("click",false);
                });
                 String num = (String) r.getProperties().get("num");
                 //随机选择红球变红的数量
                 int n = Integer.parseInt(num);
                 //在33以内随机获取n个数
                    List<Integer> list = new ArrayList<>();
                    while (list.size()<n){
                        int i = (int) (Math.random()*33+1);
                        if (!list.contains(i)){
                            list.add(i);
                        }
                    }

                for (Integer integer : list) {
                    StackPane stackPane = (StackPane) redFlowPane.getChildren().get(integer - 1);
                    //给stackPane添加一个点击事件
                    Circle circle = (Circle)stackPane.getChildren().get(0);
                    //circle.setStyle("-fx-border-color: #1678ff;");
                    Text textNode = (Text)stackPane.getChildren().get(1);
                    boolean click = (boolean) stackPane.getProperties().get("click");
                    if (click){
                        circle.setFill(Color.web("#E7E2E2FF"));
                        textNode.setStyle("-fx-font-size: 12px; -fx-font-weight: bold; -fx-fill: #4e4a4a; -fx-font-family: 'Microsoft YaHei'; -fx-font-smoothing-type: lcd; -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.2) , 1, 0.0 , 0 , 1 ); -fx-text-alignment: center; -fx-text-origin: center; -fx-font-size: 14");
                    }else{
                        circle.setFill(Color.web("#F50606FF"));
                        // circle.setStyle("-fx-border-color: #f50606;");
                        textNode.setStyle("-fx-font-size: 12px; -fx-font-weight: bold; -fx-fill: #f5f1f1; -fx-font-family: 'Microsoft YaHei'; -fx-font-smoothing-type: lcd; -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.2) , 1, 0.0 , 0 , 1 ); -fx-text-alignment: center; -fx-text-origin: center; -fx-font-size: 14");
                    }
                    stackPane.getProperties().put("click",!click);
                }
                Text lookup =(Text) vBox.lookup("#redTCount");
                if (lookup!=null){
                    lookup.setText(list.size()+"");
                }
                Text bluetext =(Text) vBox.lookup("#bulueTCount");
                if  (bluetext!=null && !bluetext.getText().equals("0")){
                    int blueCount= bluetext.getText().equals("0")?0:Integer.parseInt(bluetext.getText());
                    int redSize= list.size();
                    int countNum = combination(redSize,blueCount);
                    Text countText =(Text) vBox.lookup("#count");
                    if (countText!=null){
                        countText.setText(countNum+"");
                    }
                    Text mn =(Text) vBox.lookup("#amount");
                    if (mn!=null){
                        mn.setText(countNum*2+"");
                    }
                }
            }
        });

        ChoiceBox<String> c = new ChoiceBox<String>();//也是一个 List集合
        List<String> list = Arrays.asList("6","7","8","9","10","11","12","13","14","15","16","17","18","19","20");
        c.getItems().addAll(list);
        c.getSelectionModel().select(0);
        c.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                r.getProperties().put("num",c.getValue());
            }
        });
        randomHbox1.getChildren().addAll(r,c);
        randomHbox1.setSpacing(10);
        redGridePane.add(randomHbox1,0,2);

       GridPane.setMargin(redLabel,new Insets(10,0,0,120));
       GridPane.setMargin(redFlowPane,new Insets(10,0,0,10));
       GridPane.setMargin(randomHbox1,new Insets(10,0,0,10));

       GridPane buleGridePane = new GridPane();
       buleGridePane.setPrefWidth(hBox.getPrefWidth()*0.4);
       buleGridePane.setAlignment(Pos.TOP_LEFT);
       buleGridePane.setPrefHeight(50);
       Label blueLabel = new Label("【篮球选择区】");
       blueLabel.setFont(Font.font(15));
       blueLabel.setTextFill(Color.valueOf("blue"));
       buleGridePane.add(blueLabel,0,0);
       FlowPane blueFlowPane = new FlowPane();
        for (int i = 1; i <= 16; i++) {
            Circle circle = new Circle();
            circle.setRadius(15);
            circle.setFill(Color.web("#E7E2E2FF"));
           // circle.setStyle("-fx-border-color: #d0cdcd;");
            circle.setStroke(Color.valueOf("#D0CDCDFF"));
            circle.setStrokeWidth(1);
            circle.setId(i+"");
            StackPane stackPane = new StackPane();
            Text text = new Text(i+"");
            text.setStyle("-fx-font-size: 12px; -fx-font-weight: bold; -fx-fill: #4e4a4a; -fx-font-family: 'Microsoft YaHei'; -fx-font-smoothing-type: lcd; -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.2) , 1, 0.0 , 0 , 1 ); -fx-text-alignment: center; -fx-text-origin: center; -fx-font-size: 14");
            stackPane.getChildren().addAll(circle,text);
            stackPane.getProperties().put("click",false);
            stackPane.setOnMousePressed(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    Circle circle = (Circle)stackPane.getChildren().get(0);
                    //circle.setStyle("-fx-border-color: #1678ff;");
                    Text textNode = (Text)stackPane.getChildren().get(1);
                    boolean click = (boolean) stackPane.getProperties().get("click");
                    if (click){
                        circle.setFill(Color.web("#E7E2E2FF"));
                        text.setStyle("-fx-font-size: 12px; -fx-font-weight: bold; -fx-fill: #4e4a4a; -fx-font-family: 'Microsoft YaHei'; -fx-font-smoothing-type: lcd; -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.2) , 1, 0.0 , 0 , 1 ); -fx-text-alignment: center; -fx-text-origin: center; -fx-font-size: 14");
                    }else{
                        circle.setFill(Color.web("#1678FFFF"));
                        textNode.setStyle("-fx-font-size: 12px; -fx-font-weight: bold; -fx-fill: #f5f1f1; -fx-font-family: 'Microsoft YaHei'; -fx-font-smoothing-type: lcd; -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.2) , 1, 0.0 , 0 , 1 ); -fx-text-alignment: center; -fx-text-origin: center; -fx-font-size: 14");
                    }
                    stackPane.getProperties().put("click",!click);
                }
            });
            blueFlowPane.getChildren().add(stackPane);
            FlowPane.setMargin(stackPane,new Insets(5,0,0,10));
        }
        buleGridePane.add(blueFlowPane,0,1);
        //添加篮球随机选择
        HBox randomHbox2 = new HBox();
        Button r2 = new Button("随机选择");
        r2.setStyle("-fx-background-color: #FCF6D8FF; -fx-text-fill: #181717; -fx-font-size: 12px; -fx-border-color: #e7e5e5; -fx-border-radius: 1px; -fx-border-width: 1px; -fx-border-style: solid;");
        r2.getProperties().put("num","1");
        r2.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //清空上一次的选择结果
                blueFlowPane.getChildren().forEach(e->{
                    StackPane stackPane = (StackPane) e;
                    Circle circle = (Circle)stackPane.getChildren().get(0);
                    //circle.setStyle("-fx-border-color: #1678ff;");
                    Text textNode = (Text)stackPane.getChildren().get(1);
                    circle.setFill(Color.web("#E7E2E2FF"));
                    textNode.setStyle("-fx-font-size: 12px; -fx-font-weight: bold; -fx-fill: #4e4a4a; -fx-font-family: 'Microsoft YaHei'; -fx-font-smoothing-type: lcd; -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.2) , 1, 0.0 , 0 , 1 ); -fx-text-alignment: center; -fx-text-origin: center; -fx-font-size: 14");
                    stackPane.getProperties().put("click",false);
                });
                String num = (String) r2.getProperties().get("num");
                //随机选择红球变红的数量
                int n = Integer.parseInt(num);
                //在33以内随机获取n个数
                List<Integer> list = new ArrayList<>();
                while (list.size()<n){
                    int i = (int) (Math.random()*16+1);
                    if (!list.contains(i)){
                        list.add(i);
                    }
                }

                for (Integer integer : list) {
                    StackPane stackPane = (StackPane) blueFlowPane.getChildren().get(integer - 1);
                    //给stackPane添加一个点击事件
                    Circle circle = (Circle)stackPane.getChildren().get(0);
                    //circle.setStyle("-fx-border-color: #1678ff;");
                    Text textNode = (Text)stackPane.getChildren().get(1);
                    boolean click = (boolean) stackPane.getProperties().get("click");
                    if (click){
                        circle.setFill(Color.web("#E7E2E2FF"));
                        textNode.setStyle("-fx-font-size: 12px; -fx-font-weight: bold; -fx-fill: #4e4a4a; -fx-font-family: 'Microsoft YaHei'; -fx-font-smoothing-type: lcd; -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.2) , 1, 0.0 , 0 , 1 ); -fx-text-alignment: center; -fx-text-origin: center; -fx-font-size: 14");
                    }else{
                        circle.setFill(Color.web("#1678FFFF"));
                        textNode.setStyle("-fx-font-size: 12px; -fx-font-weight: bold; -fx-fill: #f5f1f1; -fx-font-family: 'Microsoft YaHei'; -fx-font-smoothing-type: lcd; -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.2) , 1, 0.0 , 0 , 1 ); -fx-text-alignment: center; -fx-text-origin: center; -fx-font-size: 14");
                    }
                    stackPane.getProperties().put("click",!click);
                    Text lookup =(Text) vBox.lookup("#bulueTCount");
                    if (lookup!=null){
                        lookup.setText(list.size()+"");
                    }
                    Text red =(Text) vBox.lookup("#redTCount");
                    if  (red!=null && !red.getText().equals("0")){
                        int redCount= red.getText().equals("0")?0:Integer.parseInt(red.getText());
                        int blueCount= list.size();
                        int countNum = combination(redCount,blueCount);
                        Text countText =(Text) vBox.lookup("#count");
                        if (countText!=null){
                            countText.setText(countNum+"");
                        }
                        Text mn =(Text) vBox.lookup("#amount");
                        if (mn!=null){
                            mn.setText(countNum*2+"");
                        }
                    }
                }
            }
        });

        ChoiceBox<String> c2 = new ChoiceBox<String>();//也是一个 List集合
        List<String> list2 = Arrays.asList("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16");
        c2.getItems().addAll(list2);
        c2.getSelectionModel().select(0);
        c2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                r2.getProperties().put("num",c2.getValue());
            }
        });
        randomHbox2.getChildren().addAll(r2,c2);
        randomHbox2.setSpacing(10);
        buleGridePane.add(randomHbox2,0,2);
        GridPane.setMargin(blueLabel,new Insets(10,0,0,20));
        GridPane.setMargin(blueFlowPane,new Insets(10,10,0,0));
        GridPane.setMargin(randomHbox2,new Insets(45,0,0,10));

       hBox.getChildren().addAll(redGridePane,buleGridePane);
       HBox inputHbox = new HBox();
       inputHbox.setSpacing(2);
       buildInputHBox(inputHbox,new ArrayList<>(),new ArrayList<>());


       HBox.setMargin(redGridePane,new Insets(10,0,0,0));
       HBox.setMargin(buleGridePane,new Insets(10,0,0,0));
       vBox.getChildren().addAll(hBox,inputHbox);

    }

    private  void buildInputHBox(HBox hBox,List<Integer> reds,List<Integer> blus){
            Integer r = reds.size();
            Integer b = blus.size();
            //计算双色球复试投注的注数，其中r是红球的个数，b是蓝球的个数
            int count = combination(r, b);
            int amout = count*2;
            Text t1 = new Text("您选择的红球个数是: ");
            Text t2 = new Text(reds.size()+"");
            t2.setFill(Color.RED);
            t2.setId("redTCount");
            Text t3 = new Text(" 个，蓝球个数是: ");
            Text t4 = new Text(blus.size()+"");
            t4.setFill(Color.BLUE);
            t4.setId("bulueTCount");
            Text t5 = new Text(" 个，复式选号注数: ");
            Text t6 = new Text(count+"");
            t6.setId("count");
            Text t7 = new Text(" 注， 总金额: ");
            Text t8 = new Text(amout+"");
            t8.setId("amount");
            t8.setFill(Color.RED);
            Text t9 = new Text(" 元");
            hBox.getChildren().addAll(t1,t2,t3,t4,t5,t6,t7,t8,t9);
            hBox.setSpacing(10);
            hBox.setPadding(new Insets(10,0,0,10));
    }

    private  static  int combination(int n, int k) {
        if (k == 0 || n == 0) {
            return 0;
        }
        int[][] dp = new int[n+1][k+1];
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= Math.min(i, k); j++) {
                if (j == 0 || j == i) {
                    dp[i][j] = 1;
                } else {
                    dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
                }
            }
        }
        return dp[n][k];
    }

    public static void main(String[] args) {
        System.out.println(combination(5,2));
    }

}
